package kv

import (
	"fmt"
	"mall_frontend_api/biz/config"

	"github.com/go-redis/redis"
)

var redisClient *redis.Client

func MustInit() {
		// 检查Redis开关
		if !config.Values.Redis.Switch {
			fmt.Println("Redis is disabled in config")
			return
		}

		host := fmt.Sprintf("%s:%d", config.Values.Redis.Host, config.Values.Redis.Port)
		redisConfig := &redis.Options{
			Addr:     host,
			Password: config.Values.Redis.Auth,
			DB:       0, // 默认数据库
		}

		fmt.Println("Redis connection config:", redisConfig)
		redisClient = redis.NewClient(redisConfig)

		// 测试连接
		statusCmd := redisClient.Ping()
		if statusCmd.Err() != nil {
			fmt.Printf("Failed to connect to Redis: %v\n", statusCmd.Err())
			panic(statusCmd.Err())
		}

		fmt.Println("Successfully connected to Redis")
	}

func GetRedisClient() *redis.Client {
	return redisClient
}
